<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Redis入门 | 浪飞IT小栈</title>
    <meta name="generator" content="VuePress 1.9.9">
    <link rel="icon" href="/img/favicon.ico">
    <meta name="description" content="前后端技术分享">
    <meta name="keywords" content="前端博客,后端博客,人工智能,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown,java,Spring,Springboot,Redis,mysql">
    <meta name="baidu-site-verification" content="7F55weZDDc">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/assets/css/0.styles.ada71c49.css" as="style"><link rel="preload" href="/assets/js/app.a2a00aa5.js" as="script"><link rel="preload" href="/assets/js/2.256f807a.js" as="script"><link rel="preload" href="/assets/js/60.88d94274.js" as="script"><link rel="prefetch" href="/assets/js/10.ab144fe3.js"><link rel="prefetch" href="/assets/js/11.062e0879.js"><link rel="prefetch" href="/assets/js/12.f9c60882.js"><link rel="prefetch" href="/assets/js/13.0a6ebfcf.js"><link rel="prefetch" href="/assets/js/14.03fc6f12.js"><link rel="prefetch" href="/assets/js/15.19dbf9fd.js"><link rel="prefetch" href="/assets/js/16.6e68160b.js"><link rel="prefetch" href="/assets/js/17.1d678834.js"><link rel="prefetch" href="/assets/js/18.4536fd5c.js"><link rel="prefetch" href="/assets/js/19.9c1b65df.js"><link rel="prefetch" href="/assets/js/20.c5f96e71.js"><link rel="prefetch" href="/assets/js/21.3f0b334e.js"><link rel="prefetch" href="/assets/js/22.55c868be.js"><link rel="prefetch" href="/assets/js/23.91286241.js"><link rel="prefetch" href="/assets/js/24.c1af1909.js"><link rel="prefetch" href="/assets/js/25.a995a755.js"><link rel="prefetch" href="/assets/js/26.abc5dea3.js"><link rel="prefetch" href="/assets/js/27.1eb62850.js"><link rel="prefetch" href="/assets/js/28.b383a871.js"><link rel="prefetch" href="/assets/js/29.088e0d73.js"><link rel="prefetch" href="/assets/js/3.cd716564.js"><link rel="prefetch" href="/assets/js/30.3ccc98d5.js"><link rel="prefetch" href="/assets/js/31.aab7cc23.js"><link rel="prefetch" href="/assets/js/32.5e16b7e6.js"><link rel="prefetch" href="/assets/js/33.4aaeb741.js"><link rel="prefetch" href="/assets/js/34.45e8e898.js"><link rel="prefetch" href="/assets/js/35.5899c97a.js"><link rel="prefetch" href="/assets/js/36.32e2e263.js"><link rel="prefetch" href="/assets/js/37.c4e27bbd.js"><link rel="prefetch" href="/assets/js/38.fe5e60d2.js"><link rel="prefetch" href="/assets/js/39.2cb0b4ee.js"><link rel="prefetch" href="/assets/js/4.edf4d5e7.js"><link rel="prefetch" href="/assets/js/40.08d5bead.js"><link rel="prefetch" href="/assets/js/41.2f80f24b.js"><link rel="prefetch" href="/assets/js/42.43a49913.js"><link rel="prefetch" href="/assets/js/43.08da7ec2.js"><link rel="prefetch" href="/assets/js/44.7c7cb8a9.js"><link rel="prefetch" href="/assets/js/45.0ddf7e7f.js"><link rel="prefetch" href="/assets/js/46.7c4609a5.js"><link rel="prefetch" href="/assets/js/47.edf3ddf8.js"><link rel="prefetch" href="/assets/js/48.569a90b9.js"><link rel="prefetch" href="/assets/js/49.fa368ac8.js"><link rel="prefetch" href="/assets/js/5.24054156.js"><link rel="prefetch" href="/assets/js/50.b9b3ea7f.js"><link rel="prefetch" href="/assets/js/51.a2ebf0fe.js"><link rel="prefetch" href="/assets/js/52.31f02238.js"><link rel="prefetch" href="/assets/js/53.4110ef1e.js"><link rel="prefetch" href="/assets/js/54.dab741c7.js"><link rel="prefetch" href="/assets/js/55.f27984e9.js"><link rel="prefetch" href="/assets/js/56.bd4c1a8e.js"><link rel="prefetch" href="/assets/js/57.375d40cb.js"><link rel="prefetch" href="/assets/js/58.932e3864.js"><link rel="prefetch" href="/assets/js/59.e657be1b.js"><link rel="prefetch" href="/assets/js/6.b259c061.js"><link rel="prefetch" href="/assets/js/61.fa71d884.js"><link rel="prefetch" href="/assets/js/62.a9d3b3b7.js"><link rel="prefetch" href="/assets/js/63.f849fe75.js"><link rel="prefetch" href="/assets/js/64.1770f7a9.js"><link rel="prefetch" href="/assets/js/65.6afc33db.js"><link rel="prefetch" href="/assets/js/66.799f2eb4.js"><link rel="prefetch" href="/assets/js/67.f36b2f7d.js"><link rel="prefetch" href="/assets/js/68.868cd00b.js"><link rel="prefetch" href="/assets/js/69.4c29b907.js"><link rel="prefetch" href="/assets/js/7.e34ea00f.js"><link rel="prefetch" href="/assets/js/70.1b993ed4.js"><link rel="prefetch" href="/assets/js/71.e16ad4f9.js"><link rel="prefetch" href="/assets/js/72.627505e6.js"><link rel="prefetch" href="/assets/js/73.32768e14.js"><link rel="prefetch" href="/assets/js/74.968a580d.js"><link rel="prefetch" href="/assets/js/75.42d5af3d.js"><link rel="prefetch" href="/assets/js/76.19a69d31.js"><link rel="prefetch" href="/assets/js/77.cf627a78.js"><link rel="prefetch" href="/assets/js/78.07177880.js"><link rel="prefetch" href="/assets/js/8.c4dd00ce.js"><link rel="prefetch" href="/assets/js/9.0ff73be2.js">
    <link rel="stylesheet" href="/assets/css/0.styles.ada71c49.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="浪飞IT小栈" class="logo"> <span class="site-name can-hide">浪飞IT小栈</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/avatar.jpg"> <div class="blogger-info"><h3>浪飞yes</h3> <span>无法简单的人儿~</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开篇</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础语法</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>面向对象</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>常用类</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础高级</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前端技术</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>热门框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前后端分离</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>权限控制</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>NoSQL</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/0a818f/" aria-current="page" class="active sidebar-link">Redis入门</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/0a818f/#一、今日学习内容与目标" class="sidebar-link">一、今日学习内容与目标</a></li><li class="sidebar-sub-header level2"><a href="/pages/0a818f/#二、认识-redis" class="sidebar-link">二、认识 Redis</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_2-1-数据库分类" class="sidebar-link">2.1 数据库分类</a></li><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_2-2-为什么诞生nosql" class="sidebar-link">2.2 为什么诞生NoSQL</a></li><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_2-3-什么是redis" class="sidebar-link">2.3 什么是Redis</a></li><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_2-4-谁在使用redis" class="sidebar-link">2.4 谁在使用Redis</a></li><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_2-5-redis学习网站" class="sidebar-link">2.5 Redis学习网站</a></li><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_2-6-redis安装" class="sidebar-link">2.6 Redis安装</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/0a818f/#三、redis初体验" class="sidebar-link">三、Redis初体验</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_3-1-入门案例" class="sidebar-link">3.1 入门案例</a></li><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_3-2-命令总结" class="sidebar-link">3.2 命令总结</a></li><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_3-3-redis的key设计" class="sidebar-link">3.3 Redis的Key设计</a></li><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_3-4-key的设计规范" class="sidebar-link">3.4 Key的设计规范</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/0a818f/#四、redis数据结构" class="sidebar-link">四、Redis数据结构</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_4-1-string类型" class="sidebar-link">4.1 String类型</a></li><li class="sidebar-sub-header level4"><a href="/pages/0a818f/#_4-1-1-常用命令" class="sidebar-link">4.1.1 常用命令</a></li><li class="sidebar-sub-header level4"><a href="/pages/0a818f/#_4-1-2-非常用命令" class="sidebar-link">4.1.2 非常用命令</a></li><li class="sidebar-sub-header level4"><a href="/pages/0a818f/#_4-1-3-应用场景" class="sidebar-link">4.1.3 应用场景</a></li><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_4-2-list类型" class="sidebar-link">4.2 List类型</a></li><li class="sidebar-sub-header level4"><a href="/pages/0a818f/#_4-2-1-常用命令" class="sidebar-link">4.2.1 常用命令</a></li><li class="sidebar-sub-header level4"><a href="/pages/0a818f/#_4-2-2-非常用命令" class="sidebar-link">4.2.2 非常用命令</a></li><li class="sidebar-sub-header level4"><a href="/pages/0a818f/#_4-2-2-应用场景" class="sidebar-link">4.2.2 应用场景</a></li><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_4-3-hash类型" class="sidebar-link">4.3 Hash类型</a></li><li class="sidebar-sub-header level4"><a href="/pages/0a818f/#_4-3-1-常用命令" class="sidebar-link">4.3.1 常用命令</a></li><li class="sidebar-sub-header level4"><a href="/pages/0a818f/#_4-3-2-应用场景" class="sidebar-link">4.3.2 应用场景</a></li><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_4-4-set类型" class="sidebar-link">4.4 Set类型</a></li><li class="sidebar-sub-header level4"><a href="/pages/0a818f/#_4-4-1-常用命令" class="sidebar-link">4.4.1 常用命令</a></li><li class="sidebar-sub-header level4"><a href="/pages/0a818f/#_4-4-2-非常用命令" class="sidebar-link">4.4.2 非常用命令</a></li><li class="sidebar-sub-header level4"><a href="/pages/0a818f/#_4-4-3-应用场景" class="sidebar-link">4.4.3 应用场景</a></li><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_4-5-zset类型" class="sidebar-link">4.5 zSet类型</a></li><li class="sidebar-sub-header level4"><a href="/pages/0a818f/#_4-5-1-常用命令" class="sidebar-link">4.5.1 常用命令</a></li><li class="sidebar-sub-header level4"><a href="/pages/0a818f/#_4-5-2-非常用命令" class="sidebar-link">4.5.2 非常用命令</a></li><li class="sidebar-sub-header level4"><a href="/pages/0a818f/#_4-5-3-应用场景" class="sidebar-link">4.5.3 应用场景</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/0a818f/#五、全局命令" class="sidebar-link">五、全局命令</a></li><li class="sidebar-sub-header level2"><a href="/pages/0a818f/#六、redis的安全性" class="sidebar-link">六、Redis的安全性</a></li><li class="sidebar-sub-header level2"><a href="/pages/0a818f/#七、java客户端操作redis" class="sidebar-link">七、Java客户端操作Redis</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_7-1-jedis客户端" class="sidebar-link">7.1 Jedis客户端</a></li><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_7-2-lettuce" class="sidebar-link">7.2 Lettuce</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/0a818f/#八、redis事务-拓展" class="sidebar-link">八、Redis事务[拓展]</a></li><li class="sidebar-sub-header level2"><a href="/pages/0a818f/#九、redis持久化机制-拓展" class="sidebar-link">九、Redis持久化机制[拓展]</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_9-1-rdb方式" class="sidebar-link">9.1 RDB方式</a></li><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_9-2-aof方式" class="sidebar-link">9.2 AOF方式</a></li><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_9-3-rdb-aof混合方式" class="sidebar-link">9.3 RDB-AOF混合方式</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/0a818f/#十、redis内存淘汰机制-拓展" class="sidebar-link">十、Redis内存淘汰机制[拓展]</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_10-1-引言" class="sidebar-link">10.1 引言</a></li><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_10-2-内存淘汰算法" class="sidebar-link">10.2 内存淘汰算法</a></li><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_10-3-redis淘汰策略" class="sidebar-link">10.3 Redis淘汰策略</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/0a818f/#十一、过期key处理-拓展" class="sidebar-link">十一、过期Key处理[拓展]</a></li><li class="sidebar-sub-header level2"><a href="/pages/0a818f/#十二、小结与作业" class="sidebar-link">十二、小结与作业</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_12-1-小结" class="sidebar-link">12.1 小结</a></li><li class="sidebar-sub-header level3"><a href="/pages/0a818f/#_12-2-作业" class="sidebar-link">12.2 作业</a></li></ul></li></ul></li><li><a href="/pages/4c087b/" class="sidebar-link">MongoDB</a></li><li><a href="/pages/25c0d9/" class="sidebar-link">ElasticSearch</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>微服务</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>消息中间件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>脚手架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>技术增值</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/categories/?category=Java%E9%80%9F%E9%80%9A" title="分类" data-v-06225672>Java速通</a></li><li data-v-06225672><a href="/categories/?category=NoSQL" title="分类" data-v-06225672>NoSQL</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/langfeiyes" target="_blank" title="作者" class="beLink" data-v-06225672>langfeiyes</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2024-03-11</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">Redis入门<!----></h1>  <div class="theme-vdoing-content content__default"><h1 id="redis"><a href="#redis" class="header-anchor">#</a> Redis</h1> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/redis.jpg" alt=""></p> <h2 id="一、今日学习内容与目标"><a href="#一、今日学习内容与目标" class="header-anchor">#</a> 一、今日学习内容与目标</h2> <p>1&gt;了解NoSQL概念</p> <p>2&gt;了解Redis概念，定位，优点，缺点</p> <p>3&gt;掌握Redis常用五大类型</p> <p>4&gt;掌握Redis Key 与Value值设计</p> <p>5&gt;掌握Redis全局命令</p> <p>6&gt;掌握Redis 安全与事务操作</p> <p>7&gt;了解Redis持久化策略</p> <p>8&gt;了解Redis内存淘汰机制与过期Key清理</p> <p>9&gt;掌握Java集成Redis操作</p> <p>10&gt;完成综合案例设计与实现</p> <h2 id="二、认识-redis"><a href="#二、认识-redis" class="header-anchor">#</a> 二、认识 Redis</h2> <p><strong>Redis（Remote Dictionary Server）</strong> 是一个开源的、内存中的数据结构存储系统，也可以被视为高性能的键值存储的NoSQL数据库。</p> <h3 id="_2-1-数据库分类"><a href="#_2-1-数据库分类" class="header-anchor">#</a> 2.1 数据库分类</h3> <ul><li>关系型数据库（RDBMS）
<ul><li>常用关系型数据库: Oracle，<strong>MySQL</strong>，SqlServer，DB2</li> <li>特点
<ul><li>数据存储在表格中，表格具有固定的列和行</li> <li>通常使用 SQL 作为查询语言</li> <li>具有强大的事务支持，保证数据的一致性</li></ul></li> <li>适用场景: 适用于需要复杂的关联查询和事务处理的应用场景</li></ul></li> <li>非关系型数据库（NoSQL）
<ul><li>常用的非关系数据库: <strong>Redis</strong>，MongoDB，ElasticSearch， Hbase，Neo4j</li> <li>特点:
<ul><li>数据以灵活的方式存储，可以是键值对、文档、列族、图等形式</li> <li>NoSQL 数据库通常有自己的查询语言，不一定使用 SQL</li> <li>对事务的支持可能相对较弱，允许在一定程度上放宽一致性要求，提高性能。</li></ul></li> <li>适用场景: 适用于需要处理大量非结构化或半结构化数据、对性能要求较高的应用场景</li></ul></li></ul> <h3 id="_2-2-为什么诞生nosql"><a href="#_2-2-为什么诞生nosql" class="header-anchor">#</a> 2.2 为什么诞生NoSQL</h3> <p>随着互联网 web2.0 网站的兴起，非关系型的数据库现在成了一个极其热门的新领域，系数据库产品的发展非常迅速，而传统的关系型数据库在应付 web2.0 网站，特别是超大规 模和高并发的 SNS 类型的 web2.0 纯动态网站已经显得力不从心，暴露了很多难以克服的问题，比如高并发-高效率-高扩展</p> <p><strong>1 、 Highperformance- 对数据库高并发读写的需求</strong></p> <p>web2.0 网站要根据用户个性化信息来实时生成动态页面和提供动态信息，所以基本上无法使用动态页面静态化技术，因此数据库并发负载非常高，往往要达到每秒上万次读写请求。关系型数据库应付上万次 SQL 查询还勉强顶得住，但是应付上万次 SQL 写数据请求，硬盘IO 就已经无法承受了，其实对于普通的 BS 网站，往往也存在对高并发写请求的需求。</p> <p><strong>2 、HugeStorage- 对海量数据的高效率存储和访问的需求</strong></p> <p>对于大型的 SNS 网站，每天用户产生海量的用户动态信息，以国外的 Friend feed 为例，一个月就达到了 2.5 亿条用户动态，对于关系数据库来说，在一张 2.5 亿条记录的表里面进行SQL 查询，效率是极其低下乃至不可忍受的。再例如大型 web 网站的用户登录系统，例如腾讯，盛大，动辄数以亿计的帐号，关系数据库也很难应付。</p> <p><strong>3、HighScalability&amp;&amp;HighAvailability- 对数据库的高可扩展性和高可用性的需求</strong></p> <p>在基于 web 的架构当中，数据库是最难进行横向扩展的，当一个应用系统的用户量和访问量与日俱增的时候，你的数据库却没有办法像 Web server 和 App server 那样简单的通过添加更多的硬件和服务节点来扩展性能和负载能力。对于很多需要提供 24 小时不间断服务的网站来说，对数据库系统进行升级和扩展是非常痛苦的事情，往往需要停机维护和数据迁移，可是停机维护随之带来的就是公司收入的减少</p> <h3 id="_2-3-什么是redis"><a href="#_2-3-什么是redis" class="header-anchor">#</a> 2.3 什么是Redis</h3> <p>Redis是以key-value形式存储,和传统的关系型数据库不一样.不一定遵循传统数据库的一些基本要求</p> <ul><li><p>优点:</p> <ul><li><p><strong>快速的读写速度</strong>: Redis 将数据存储在内存中，因此具有非常快速的读写速度。内存中的存储和高效的数据结构使得 Redis 能够在毫秒级别提供响应，下面是官方的bench-mark数据：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>测试完成了50个并发执行100000个请求。
设置和获取的值是一个256字节字符串。
Linux box是运行Linux 2.6,这是X3320 Xeon 2.5 ghz。
文本执行使用loopback接口(127.0.0.1)。
结果:读的速度是110000次/s,写的速度是81000次/s 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li> <li><p><strong>单线程模型</strong>：Redis 使用单线程来处理命令，这使得它能够避免锁的竞争和上下文切换的开销。</p></li> <li><p><strong>支持多种数据结构</strong>：Redis 支持多种数据结构，如字符串、哈希表、列表、集合和有序集合等。这样的灵活性使得开发者可以根据应用场景选择合适的数据结构，提高了数据处理的效率。</p></li> <li><p><strong>持久化和复制</strong>：Redis 提供多种持久化选项，如 RDB 持久化和 AOF 持久化，以及主从复制机制，支持数据的持久性和可靠性。</p></li></ul></li> <li><p>缺点:</p> <ul><li><strong>内存占用</strong>: 为 Redis 数据存储在内存中，对于大规模数据集，可能需要较大的内存空间。</li> <li><strong>不适合复杂查询</strong>: Redis 不支持复杂的查询语言，适用于简单的键值对操作，但不适用于复杂查询和关系型数据库的联结操作。</li></ul></li></ul> <h3 id="_2-4-谁在使用redis"><a href="#_2-4-谁在使用redis" class="header-anchor">#</a> 2.4 谁在使用Redis</h3> <p><strong>比较著名的公司有：</strong></p> <ul><li>github (全球最大的代码托管平台)</li> <li>blizzard (暴雪娱乐)</li> <li>stackoverflow (全球IT技术问答平台)</li> <li>flickr（雅虎旗下图片分享网站）</li></ul> <p><strong>国内:</strong></p> <ul><li><p>新浪微博</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>新浪微博拥有全球最大的Redis集群【2017】
2200+亿commands/day 5000亿Read/day 500亿Write/day
18TB+ Memory
500+ Servers in 6 IDC 2000+instances
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></li> <li><p>淘宝</p></li> <li><p>字节跳动</p></li> <li><p>美团</p></li></ul> <h3 id="_2-5-redis学习网站"><a href="#_2-5-redis学习网站" class="header-anchor">#</a> 2.5 Redis学习网站</h3> <p>Redis在线入门 ： http://try.redis.io/</p> <p>Redis 中文资料站： http://www.redis.cn/</p> <p>Redis 教程 : https://www.runoob.com/redis/redis-tutorial.html</p> <p>Redis 命令参考 : http://doc.redisfans.com/</p> <h3 id="_2-6-redis安装"><a href="#_2-6-redis安装" class="header-anchor">#</a> 2.6 Redis安装</h3> <p>Redis是使用C写的程序，目前主流在Linux操作系统，官方不提供Window版，最新的Redis版：Redis7.x</p> <p>官网下载：https://Redis.io/download/</p> <p>Redis 的Window版是微软公司自行移植的，目前最新版：Redis-x64-5.0.14.1.msi</p> <p>window版：https://github.com/tporadowski/redis/releases</p> <p>由于我们还没有讲Linux，所以我们暂时安装Window版本的.</p> <p>注意事项:</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240111104913486.png" alt=""></strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240111105032373.png" alt=""></p> <p>验证是否安装成功 win+R 打开运行窗口,输入redis-cli</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240111105206534.png" alt=""></strong></p> <p>出现如下界面说明安装成功:</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240111105251367.png" alt=""></strong></p> <h2 id="三、redis初体验"><a href="#三、redis初体验" class="header-anchor">#</a> 三、Redis初体验</h2> <p>我们今天主要学习Redis的命令行操作，熟悉常用的数据结构之后。我们在后续的项目会使用Java的客户端连接Redis（通过框架提供的方法操作Redis命令进行存取操作）进行相关业务逻辑处理。</p> <h3 id="_3-1-入门案例"><a href="#_3-1-入门案例" class="header-anchor">#</a> 3.1 入门案例</h3> <p>大家可以简单理解Redis就是一个非常大的Map，那么我们需要操作它就需要有<strong>key</strong>和<strong>value</strong>，我们对于Redis的操作主要就是<strong>存数据</strong>和<strong>取数据</strong></p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">## 查看Redis中目前都有哪些Key</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> keys *
<span class="token punctuation">(</span>empty list or <span class="token builtin class-name">set</span><span class="token punctuation">)</span>
<span class="token comment">## 往Redis中存储key=name,value=wolfcode的数据</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> <span class="token builtin class-name">set</span> name wolfcode
OK
<span class="token comment">## 往Redis中存储key=age,value=18的数据</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> <span class="token builtin class-name">set</span> age <span class="token number">18</span>
OK
<span class="token comment">## 查看Redis中目前都有哪些Key</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> keys *
<span class="token number">1</span><span class="token punctuation">)</span> <span class="token string">&quot;name&quot;</span>
<span class="token number">2</span><span class="token punctuation">)</span> <span class="token string">&quot;age&quot;</span>
<span class="token comment">## 查看Redis中name的值是啥</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> get name
<span class="token string">&quot;wolfcode&quot;</span>
<span class="token comment">## 查看Redis中age的值是啥</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> get age
<span class="token string">&quot;18&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p>如果想要修改原来的数据，那么就是覆盖这个key所对应的value即可</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">## 往Redis中存储key=name,value=java的数据,此时会覆盖原来的数据</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> <span class="token builtin class-name">set</span> name <span class="token function">java</span>
OK
<span class="token comment">## 查看Redis中name的值是啥</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> get name
<span class="token string">&quot;java&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>对于不需要的数据，我们可以从Redis中把数据删除掉</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">## 在Redis中删除key=age的数据</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> del age
<span class="token punctuation">(</span>integer<span class="token punctuation">)</span> <span class="token number">1</span>
<span class="token comment">## 查看Redis都有哪些Key</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> keys *
<span class="token number">1</span><span class="token punctuation">)</span> <span class="token string">&quot;name&quot;</span>
<span class="token comment">## 获取Redis中不存在的key会返回nil</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> get age
<span class="token punctuation">(</span>nil<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>因为Redis是将数据存储到内存中的，随着时间的推移，业务的增多，那么服务器的内存占用也会越来越多. 对于某些数据刚发布的时候，访问比较多，比较频繁，所以我们可能会把这些数据存储到Redis中从而提供用户的访问速度，随着时间推移，这些数据用户可能不再访问了，但是依然占用着内存。Redis就提供了数据的过期时间的设置，给key设置过期时间之后，到了时间会自动删除掉该Key并释放内存。</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">## 往Redis中存储key=sex,value=man的数据,数据有效时间为60秒</span>
<span class="token builtin class-name">set</span> sex <span class="token function">man</span> EX <span class="token number">60</span>
<span class="token comment">## 查看Redis中sex的值是啥</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> get sex
<span class="token string">&quot;man&quot;</span>
<span class="token comment">## 查看sex的剩余有效时间</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> ttl sex
<span class="token punctuation">(</span>integer<span class="token punctuation">)</span> <span class="token number">43</span>
<span class="token comment">## 当sex的数据过期之后，通过ttl查看剩余有效时间</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> ttl sex
<span class="token punctuation">(</span>integer<span class="token punctuation">)</span> <span class="token parameter variable">-2</span>
<span class="token comment">## 查看name的剩余有效时间，如果没有设置过期时间，ttl返回的是-1</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> ttl name
<span class="token punctuation">(</span>integer<span class="token punctuation">)</span> <span class="token parameter variable">-1</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h3 id="_3-2-命令总结"><a href="#_3-2-命令总结" class="header-anchor">#</a> 3.2 命令总结</h3> <table><thead><tr><th>命令</th> <th>解释</th> <th>示例</th></tr></thead> <tbody><tr><td>keys  pattern</td> <td>按照pattern 匹配规则，列表Redis中所有的key</td> <td>keys *</td></tr> <tr><td>set key value</td> <td>将key-value缓存redis中</td> <td>set name wolfcode</td></tr> <tr><td>get key</td> <td>从Redis中获取key对应value值</td> <td>get name</td></tr> <tr><td>del key</td> <td>删除一个key</td> <td>del age</td></tr> <tr><td>set key value EX 有效秒数</td> <td>设置key=value得同时设置数据有效时间</td> <td>set sex man EX 60</td></tr> <tr><td>ttl key</td> <td>查看某个key有效时间，如果没有设置有效时间返回-1，如果该key已经过期了，返回-2</td> <td>ttl sex</td></tr></tbody></table> <h3 id="_3-3-redis的key设计"><a href="#_3-3-redis的key设计" class="header-anchor">#</a> 3.3 Redis的Key设计</h3> <p>现在我们有这样一张商品表</p> <table><thead><tr><th>product_id</th> <th>product_name</th> <th>product_image</th> <th>price</th> <th>quantity</th></tr></thead> <tbody><tr><td>1</td> <td>Laptop</td> <td>laptop_image.jpg</td> <td>2999</td> <td>50</td></tr></tbody></table> <p>商品信息是很高频访问的，我们考虑提升系统性能，把商品数据存储到Redis中，用户查询的时候，我们就从Redis中获取即可. 那么我们应该如何往Redis中存储数据呢?</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> <span class="token builtin class-name">set</span> product_id <span class="token number">1</span>
OK
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> <span class="token builtin class-name">set</span> product_name <span class="token string">&quot;Laptop&quot;</span>
OK
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> <span class="token builtin class-name">set</span> product_image <span class="token string">&quot;laptop_image.jpg&quot;</span>
OK
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> <span class="token builtin class-name">set</span> price <span class="token number">2999</span>
OK
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> <span class="token builtin class-name">set</span> quantity <span class="token number">50</span>
OK
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>但是我们商品表中不止一条数据，如果是下面的情况，我们应该如何往Redis中存储数据呢?</p> <table><thead><tr><th>product_id</th> <th>product_name</th> <th>product_image</th> <th>price</th> <th>quantity</th></tr></thead> <tbody><tr><td>1</td> <td>Laptop</td> <td>laptop_image.jpg</td> <td>2999</td> <td>50</td></tr> <tr><td>2</td> <td>Coffee Maker</td> <td>coffee_maker_image.jpg</td> <td>599</td> <td>200</td></tr></tbody></table> <p>考虑到Redis如果key相同的情况会进行覆盖，所以我们对Key做了一些处理</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> <span class="token builtin class-name">set</span> product_name:1 <span class="token string">&quot;Laptop&quot;</span>
OK
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> <span class="token builtin class-name">set</span> product_image:1 <span class="token string">&quot;laptop_image.jpg&quot;</span>
OK
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> <span class="token builtin class-name">set</span> price:1 <span class="token number">2999</span>
OK
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> <span class="token builtin class-name">set</span> quantity:1 <span class="token number">50</span>
OK
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> <span class="token builtin class-name">set</span> product_name:2 <span class="token string">&quot;Coffee Maker&quot;</span>
OK
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> <span class="token builtin class-name">set</span> product_image:2 <span class="token string">&quot;coffee_maker_image.jpg&quot;</span>
OK
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> <span class="token builtin class-name">set</span> price:2 <span class="token number">599</span>
OK
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> <span class="token builtin class-name">set</span> quantity:2 <span class="token number">200</span>
OK
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>我们可以在原有的key上面添加主键(唯一标识)来区分不同数据内容,但是这样的话，当我们需要查询1号商品数据，我们需要这样操作.</p> <p>1.根据主键=1，拼接字符串，从Redis中获取数据</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> get product_name:1 
<span class="token string">&quot;Laptop&quot;</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> get product_image:1 
<span class="token string">&quot;laptop_image.jpg&quot;</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> get price:1 
<span class="token string">&quot;2999&quot;</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> get quantity:1 
<span class="token string">&quot;50&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><ol start="2"><li>取出数据之后封装成Product对象，然会给用户.</li></ol> <p>这样操作，如果Product对象有很多的字段，那么我们获取数据的时候，需要访问Redis很多次，才能获取到完整的Product数据，所以我们一般是这样操作的.</p> <p>1.将对象的JSON字符串存储到Redis中</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">## 将id=1的商品数据存储到Redis中</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> SET product:1 <span class="token string">&quot;{<span class="token entity" title="\&quot;">\&quot;</span>product_id<span class="token entity" title="\&quot;">\&quot;</span>:1,<span class="token entity" title="\&quot;">\&quot;</span>product_name<span class="token entity" title="\&quot;">\&quot;</span>:<span class="token entity" title="\&quot;">\&quot;</span>Laptop<span class="token entity" title="\&quot;">\&quot;</span>,<span class="token entity" title="\&quot;">\&quot;</span>product_image<span class="token entity" title="\&quot;">\&quot;</span>:<span class="token entity" title="\&quot;">\&quot;</span>laptop_image.jpg<span class="token entity" title="\&quot;">\&quot;</span>,<span class="token entity" title="\&quot;">\&quot;</span>price<span class="token entity" title="\&quot;">\&quot;</span>:2999,<span class="token entity" title="\&quot;">\&quot;</span>quantity<span class="token entity" title="\&quot;">\&quot;</span>:50}&quot;</span>
OK
<span class="token comment">## 将id=2的商品数据存储到Redis中</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> SET product:2 <span class="token string">&quot;{<span class="token entity" title="\&quot;">\&quot;</span>product_id<span class="token entity" title="\&quot;">\&quot;</span>:2,<span class="token entity" title="\&quot;">\&quot;</span>product_name<span class="token entity" title="\&quot;">\&quot;</span>:<span class="token entity" title="\&quot;">\&quot;</span>Coffee Maker<span class="token entity" title="\&quot;">\&quot;</span>,<span class="token entity" title="\&quot;">\&quot;</span>product_image<span class="token entity" title="\&quot;">\&quot;</span>:<span class="token entity" title="\&quot;">\&quot;</span>coffee_maker_image.jpg<span class="token entity" title="\&quot;">\&quot;</span>,<span class="token entity" title="\&quot;">\&quot;</span>price<span class="token entity" title="\&quot;">\&quot;</span>:3999,<span class="token entity" title="\&quot;">\&quot;</span>quantity<span class="token entity" title="\&quot;">\&quot;</span>:200}&quot;</span>
OK
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>2.我们获取的时候,只需要从Redis中获取一次就可以了.</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> get product:1
<span class="token string">&quot;{<span class="token entity" title="\&quot;">\&quot;</span>product_id<span class="token entity" title="\&quot;">\&quot;</span>:1,<span class="token entity" title="\&quot;">\&quot;</span>product_name<span class="token entity" title="\&quot;">\&quot;</span>:<span class="token entity" title="\&quot;">\&quot;</span>Laptop<span class="token entity" title="\&quot;">\&quot;</span>,<span class="token entity" title="\&quot;">\&quot;</span>product_image<span class="token entity" title="\&quot;">\&quot;</span>:<span class="token entity" title="\&quot;">\&quot;</span>laptop_image.jpg<span class="token entity" title="\&quot;">\&quot;</span>,<span class="token entity" title="\&quot;">\&quot;</span>price<span class="token entity" title="\&quot;">\&quot;</span>:2999,<span class="token entity" title="\&quot;">\&quot;</span>quantity<span class="token entity" title="\&quot;">\&quot;</span>:50}&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>3.获取到JSON字符串之后，在程序中转成对象即可.</p> <h3 id="_3-4-key的设计规范"><a href="#_3-4-key的设计规范" class="header-anchor">#</a> 3.4 Key的设计规范</h3> <p>我们在使用Redis的时候，需要定义很多的Key，那么定义这些Key的时候有哪些规范呢?</p> <ul><li><strong>唯一性：</strong> Key 应该具有唯一性，以确保不同类型的数据不会发生冲突。确保 Key 的命名足够明确，以防止数据误用。</li> <li><strong>简洁性：</strong> 尽量保持 Key 的简洁，避免使用过长或过于复杂的 Key。短小的 Key 有助于减小内存占用和提高性能。</li> <li><strong>避免特殊字符：</strong> 避免在 Key 中使用特殊字符，如空格或控制字符。这有助于避免由于特殊字符而引起的问题。</li> <li><strong>命名空间：</strong> 可以通过在 Key 中使用命名空间来组织数据。例如，可以使用 &quot;user:123&quot; 表示用户数据，&quot;product:456&quot; 表示产品数据</li> <li><strong>可读性：</strong> Key 的命名应该具有可读性，以便开发人员和维护人员能够轻松理解和管理。采用有意义的命名规范，例如使用命名空间和层次结构，对于用户数据，可以选择使用 &quot;user:123&quot; 而不是 &quot;u:123&quot;</li> <li><strong>避免过期 Key：</strong> 如果数据有固定的生命周期，可以使用过期时间（TTL）来设置 Key 的过期时间，以防止无用数据长时间存储</li></ul> <p><strong>总结起来：唯一性，可读性，灵活性，失效性</strong></p> <h2 id="四、redis数据结构"><a href="#四、redis数据结构" class="header-anchor">#</a> 四、Redis数据结构</h2> <p>Redis支持的存储数据类型有很多：</p> <p><strong>常用</strong>：</p> <p>string（字符串，hash（哈希），list（列表），set（集合），zset (sorted set：有序集合)</p> <p><strong>不常用</strong>：</p> <p>HyperLogLog，Bitmap(位图)，Bloom Filter(布隆过滤器)，Geospatial(地理位置) ，Module(模块)， Streams(流信息)</p> <p>我们主要讲解常用的五种数据类型.</p> <h3 id="_4-1-string类型"><a href="#_4-1-string类型" class="header-anchor">#</a> 4.1 String类型</h3> <p>String类型是Redis用的较多的一个基本类型，也是最简单的一种类型，它和我们在Java中使用的字符类型什么太大区别</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/1666682-20211020192622689-1303968847.png" alt=""></strong></p> <h4 id="_4-1-1-常用命令"><a href="#_4-1-1-常用命令" class="header-anchor">#</a> 4.1.1 常用命令</h4> <table><thead><tr><th>命令格式</th> <th>功能</th> <th>案例</th></tr></thead> <tbody><tr><td>set key value</td> <td>将key-value缓存redis中</td> <td>set name wolfcode</td></tr> <tr><td>get key</td> <td>从redis中获取key对应value值</td> <td>get name</td></tr> <tr><td>del  key</td> <td>从redis中删除key</td> <td>del name</td></tr> <tr><td>ttl key</td> <td>查看key存活时间</td> <td>ttl sex</td></tr> <tr><td>mset key1 value1 key2 value2 ...</td> <td>批量设置值</td> <td>mset name wolfcode age 18</td></tr> <tr><td>mget key1 key2 ...</td> <td>批量获取值</td> <td>mget name age</td></tr> <tr><td>incr key</td> <td>对指定的key对应的value进行原子性递增(value必须是int类型)</td> <td>incr age</td></tr> <tr><td>decr key</td> <td>对指定的key对应的value进行原子性递减</td> <td>decr age</td></tr> <tr><td>setex key seconds value</td> <td>将key-value缓存到redis中，seconds 秒后失效</td> <td>setex  sex  10  man</td></tr> <tr><td>setnx key value</td> <td>如果key已经存，不做任何操作返回0，如果key不存，直接添加返回1</td> <td>setnx  name java</td></tr></tbody></table> <h4 id="_4-1-2-非常用命令"><a href="#_4-1-2-非常用命令" class="header-anchor">#</a> 4.1.2 非常用命令</h4> <table><thead><tr><th>命令格式</th> <th>功能</th> <th>案例</th></tr></thead> <tbody><tr><td>incrby key increment</td> <td>给key对应值加increment</td> <td>incrby age 10</td></tr> <tr><td>append key  value</td> <td>在key对应的value值中拼+value</td> <td>append name yes</td></tr> <tr><td>setrange key  offset  value</td> <td>修改key对应的value值,替换为指定value,冲offset索引位置开始</td> <td>setrange name 2   xx</td></tr></tbody></table> <h4 id="_4-1-3-应用场景"><a href="#_4-1-3-应用场景" class="header-anchor">#</a> 4.1.3 应用场景</h4> <ul><li><p><strong>分布式缓存</strong></p> <p>首先最基本的就是用来做业务数据的缓存，如图所示，Redis中会缓存一些常用的热点数据，可以提升数据查询的性能。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/1666682-20211020192624192-635130158.png" alt=""></strong></p></li> <li><p><strong>分布式全局ID</strong>:</p> <p>使用String类型的incr命令，实现原子递增</p></li> <li><p><strong>限流</strong>:</p> <p>使用计数器实现手机验证码频率限流</p></li> <li><p><strong>分布式session</strong>:</p> <p>基于登录场景中，保存token信息。</p></li></ul> <h3 id="_4-2-list类型"><a href="#_4-2-list类型" class="header-anchor">#</a> 4.2 List类型</h3> <p>列表类型(list)可以存储一个有序且可重复的字符串列表，常用的操作是向列表两端添加元素或者获得列表的某一个片段</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/1666682-20211020192624370-2094512803.png" alt=""></strong></p> <h4 id="_4-2-1-常用命令"><a href="#_4-2-1-常用命令" class="header-anchor">#</a> 4.2.1 常用命令</h4> <table><thead><tr><th>命令格式</th> <th>功能</th> <th>案例</th></tr></thead> <tbody><tr><td>lpush key value</td> <td>从队列的左边入队列一个或者多个元素</td> <td>lpush hobby java</td></tr> <tr><td>rpush  key  value</td> <td>从队列的右边入队列一个或者多个元素</td> <td>rpush hobby python</td></tr> <tr><td>llen key</td> <td>获取列表长度</td> <td>llen hobby</td></tr> <tr><td>lrange key start stop</td> <td>返回从左边开始列表key集合，从start位置开始，stop位置结束</td> <td>lrange hobby 0 -1</td></tr> <tr><td>lpop key</td> <td>从队列的左边出队列一个元素</td> <td>lpop hobby</td></tr> <tr><td>rpop key</td> <td>弹出key集合中最右边的数据</td> <td>rpop hobby</td></tr></tbody></table> <h4 id="_4-2-2-非常用命令"><a href="#_4-2-2-非常用命令" class="header-anchor">#</a> 4.2.2 非常用命令</h4> <table><thead><tr><th>命令格式</th> <th>功能</th> <th>案例</th></tr></thead> <tbody><tr><td>linsert key before pivot value</td> <td>操作key集合，在pivot值之前插入value</td> <td>linsert hobby before java  c##</td></tr> <tr><td>linsert key  after  pivot value</td> <td>操作key集合，在pivot值之后插入value</td> <td>linsert hobby after  java  c##</td></tr> <tr><td>lset key  index  value</td> <td>操作key集合，更新索引index位置值为value</td> <td>lset hobby 1  go</td></tr> <tr><td>lrem key count  value</td> <td>操作key集合，删除 count个 value值</td> <td>lrem hobby 3   go</td></tr> <tr><td>ltrim   key  start stop</td> <td>操作key集合，从start到stop截取自列表</td> <td>ltrim  hobby 2   4</td></tr> <tr><td>lindex  key  index</td> <td>操作key集合，获取索引为index位置的数据</td> <td>lindex  hobby 1</td></tr> <tr><td>blpop key timeout</td> <td>删除，并获取该列表左边第一个元素，如果当前队列没有元素则阻塞，知道有新的元素入队</td> <td>blpop hobby timeout</td></tr> <tr><td>brpop key timeout</td> <td>删除，并获取该列表右边第一个元素，如果当前队列没有元素则阻塞，知道有新的元素入队</td> <td>brpop hobby timeout</td></tr> <tr><td>rpoplpush source destination</td> <td>命令rpoplpush在一个原子时间内，执行以下两个动作:  1.将列表source中的最后一个元素(尾元素)弹出，并返回给客户端。2.将source弹出的元素插入到列表destination，作为destination列表的头元素</td> <td>rpoplpush hobby new_hobby</td></tr></tbody></table> <h4 id="_4-2-2-应用场景"><a href="#_4-2-2-应用场景" class="header-anchor">#</a> 4.2.2 应用场景</h4> <ul><li><p><strong>发红包的场景</strong></p> <p>在发红包的场景中，假设发一个10元，10个红包，需要保证抢红包的人不会多抢到，也不会少抢到，这种情况下，可以根据下图所示去实现。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/1666682-20211020192627094-337269500.png" alt=""></strong></p></li> <li><p><strong>朋友圈的点赞列表</strong></p> <p>有好友点赞之后，把好友的头像信息使用List存储起来</p></li></ul> <h3 id="_4-3-hash类型"><a href="#_4-3-hash类型" class="header-anchor">#</a> 4.3 Hash类型</h3> <p>Hash类型大家应该都不陌生，他就是一个键值对集合，如下图所示。Hash相当于一个 string 类型的 key和 value 的映射表，key 还是key，但是value是一个键值对（key-value），类比于 Java里面的 Map&lt;String,Map&lt;String,Object&gt;&gt; 集合。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/1666682-20211020192627349-218916856.png" alt=""></strong></p> <h4 id="_4-3-1-常用命令"><a href="#_4-3-1-常用命令" class="header-anchor">#</a> 4.3.1 常用命令</h4> <table><thead><tr><th>命令格式</th> <th>功能</th> <th>案例</th></tr></thead> <tbody><tr><td>hset key field  value</td> <td>将field  value对缓存到redis中hash中，键值为key</td> <td>hset user name wolfcode</td></tr> <tr><td>hget key field</td> <td>从key对应hash列表中获取field字段</td> <td>hget user  name</td></tr> <tr><td>hexists key  field</td> <td>判断key对应的hash列表是否存在 field字段</td> <td>hexists user age</td></tr> <tr><td>hdel key  field</td> <td>删除key对应hash列表中field字段</td> <td>hdel user age</td></tr> <tr><td>hincrby  key  field  increment</td> <td>给key对应hash列表中field字段 + increment</td> <td>hincrby user  age 10</td></tr> <tr><td>hlen key</td> <td>查看key对应的hash列表field的数量</td> <td>hlen user</td></tr> <tr><td>hkeys  key</td> <td>获取key对应的hash列表所有的field值</td> <td>hkeys  user</td></tr> <tr><td>hvals  key</td> <td>获取key对应的hash列表所有的field对应的value值</td> <td>hvals  user</td></tr> <tr><td>hgetall key</td> <td>获取key对应的hash列表中所有的field及其对应的value值</td> <td>hgetall user</td></tr></tbody></table> <h4 id="_4-3-2-应用场景"><a href="#_4-3-2-应用场景" class="header-anchor">#</a> 4.3.2 应用场景</h4> <ul><li><p><strong>购物车功能</strong></p> <p>以用户ID作为key，以商品id作为field，以商品的数量作为value</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240111151422478.png" alt=""></strong></p></li> <li><p><strong>配置信息</strong></p> <p>我们有需求要获取所有的配置信息，也有需求只想获取某个名称的配置信息，这种情况使用Hash比较合适.</p></li></ul> <h3 id="_4-4-set类型"><a href="#_4-4-set类型" class="header-anchor">#</a> 4.4 Set类型</h3> <p>如下图所示，集合类型 (Set) 是一个无序并唯一的键值集合。它的存储顺序不会按照插入的先后顺序进行存储。</p> <p>集合类型和列表类型的区别如下：</p> <ul><li>列表可以存储重复元素，集合只能存储非重复元素；</li> <li>列表是按照元素的先后顺序存储元素的，而集合则是无序方式存储元素的。</li></ul> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/1666682-20211020192629214-788081891.png" alt=""></strong></p> <h4 id="_4-4-1-常用命令"><a href="#_4-4-1-常用命令" class="header-anchor">#</a> 4.4.1 常用命令</h4> <table><thead><tr><th>命令格式</th> <th>功能</th> <th>案例</th></tr></thead> <tbody><tr><td>sadd key  members [....]</td> <td>往key 集合中添加member元素</td> <td>sadd myset a  b  c b</td></tr> <tr><td>scard key</td> <td>获取集合里面的元素数量</td> <td>scard myset</td></tr> <tr><td>smembers key</td> <td>遍历key集合中所有的元素</td> <td>smembers myset</td></tr> <tr><td>sismember key member</td> <td>判断 member元素是否集合 key 的成员。</td> <td>sismember myset c</td></tr> <tr><td>srem  key members [....]</td> <td>删除key集合中members元素</td> <td>srem myset a</td></tr> <tr><td>spop key count</td> <td>从key集合中随机弹出count个元素</td> <td>spop myset 1</td></tr></tbody></table> <h4 id="_4-4-2-非常用命令"><a href="#_4-4-2-非常用命令" class="header-anchor">#</a> 4.4.2 非常用命令</h4> <table><thead><tr><th>命令格式</th> <th>功能</th> <th>案例</th></tr></thead> <tbody><tr><td><strong>sdiff key1   key2</strong></td> <td><strong>返回key1中特有的元素的差集</strong></td> <td><strong>sdiff key1 key2</strong></td></tr> <tr><td>sdiffstore  dest  key1 key2</td> <td>返回key1中特有的元素，并将返回值缓存到dest集合中</td> <td>sidiffstore  dest  key1 key2</td></tr> <tr><td><strong>sinter key1 key2</strong></td> <td><strong>返回key1跟key2集合的交集</strong></td> <td><strong>sinter key1 key2</strong></td></tr> <tr><td>sinterstore  dest key1 key2</td> <td>返回key1跟key2集合的交集，并将返回值缓存到dest集合中</td> <td>sinterstore  dest key1 key2</td></tr> <tr><td><strong>sunion key1  key2</strong></td> <td><strong>返回key1跟key2集合的并集</strong></td> <td><strong>sunion key1  key2</strong></td></tr> <tr><td>sunionstore dest key1  key2</td> <td>返回key1跟key2集合的并集，并将返回值缓存到dest集合中</td> <td>sunionstore dest key1  key2</td></tr> <tr><td>smove source destination member</td> <td>将source集合中member元素移动到destination集合中</td> <td>smove key1  key2 aa</td></tr> <tr><td>srandmember  key  count</td> <td>随机获取set集合中count 个元素</td> <td>srandmember key1 1</td></tr></tbody></table> <h4 id="_4-4-3-应用场景"><a href="#_4-4-3-应用场景" class="header-anchor">#</a> 4.4.3 应用场景</h4> <ul><li><p>去重</p></li> <li><p>抽奖</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>1：准备一个抽奖池:
sadd luckydraw Richard Tony Lily Sophia Zoe Thomas Anne Lucy Jack Tom
2：抽3个三等奖:spop luckydraw 3
3：抽2个二等奖:spop luckydraw 2
4：抽1个:一等奖:spop luckydraw 1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li> <li><p>好友推荐功能</p> <ol><li><p>给用户添加标签。</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>sadd user:1:basketball game coding swing
sadd user:2:sing coding <span class="token function">sleep</span> basketball
<span class="token punctuation">..</span>.
sadd user:k:tags tag1 tag2 tag4
<span class="token punctuation">..</span>.
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li> <li><p>使用sinter命令，可以来计算用户共同感兴趣的标签</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>sinter user:1 user:2
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li></ol> <p>这种标签系统在电商系统、社交系统、视频网站，图书网站，旅游网站等都有着广泛的应用。例如一个用户可能对娱乐、体育比较感兴趣，另一个用户可能对历史、新闻比较感兴趣，</p> <p>这些兴趣点就是标签。有了这些数据就可以得到喜欢同一个标签的人，以及用户的共同喜好的标签，这些数据对于用户体验以及增强用户黏度比较重要。</p> <p>例如一个社交系统可以根据用户的标签进行好友的推荐，已经用户感兴趣的新闻的推荐等，一个电子商务的网站会对不同标签的用户做不同类型的推荐，比如对数码产品比较感兴趣的人，</p> <p>在各个页面或者通过邮件的形式给他们推荐最新的数码产品，通常会为网站带来更多的利益</p></li></ul> <h3 id="_4-5-zset类型"><a href="#_4-5-zset类型" class="header-anchor">#</a> 4.5 zSet类型</h3> <p>有序集合类型，顾名思义，和前面讲的集合类型的区别就是多了有序的功能。</p> <p>如下图所示，在集合类型的基础上，有序集合类型为集合中的每个元素都关联了一个分数（浮点型），这使得我们不仅可以完成插入、删除和判断元素是否存在等集合类型支持的操作，还能获得分数最高(或最低)的前N个元素、获得指定分数范围内的元素等与分数有关的操作。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/1666682-20211020192630282-68030134.png" alt=""></strong></p> <h4 id="_4-5-1-常用命令"><a href="#_4-5-1-常用命令" class="header-anchor">#</a> 4.5.1 常用命令</h4> <table><thead><tr><th>命令格式</th> <th>功能</th> <th>案例</th></tr></thead> <tbody><tr><td>zadd key score member</td> <td>往key集合中添加member元素，分数为score</td> <td>zadd players 100  a</td></tr> <tr><td>zincrby  key increment  member</td> <td>将key集合中的member元素 分数 + increment</td> <td>zadd players 100  a</td></tr> <tr><td>zrange  key  start  stop [withscores]</td> <td>将key集合中的元素按分数升序排列 [显式分数]</td> <td>zrange players 0 -1  withscores</td></tr> <tr><td>zrevrange key  start  stop [withscores]</td> <td>将key集合中的元素按分数降序排列 [显式分数]</td> <td>zrevrange players 0 -1  withscores</td></tr> <tr><td>zrank  key  member</td> <td>返回member元素在key结合中的正序排名</td> <td>zrank players  a</td></tr> <tr><td>zrevrank key  member</td> <td>返回member元素在key结合中的倒序排名</td> <td>zrevrank players  a</td></tr> <tr><td>zcard  key</td> <td>返回key集合元素个数</td> <td>zcard  players</td></tr></tbody></table> <h4 id="_4-5-2-非常用命令"><a href="#_4-5-2-非常用命令" class="header-anchor">#</a> 4.5.2 非常用命令</h4> <table><thead><tr><th>命令格式</th> <th>功能</th> <th>案例</th></tr></thead> <tbody><tr><td>zrangebyscore  key  min  max  [withscores]</td> <td>按[min, max) 分数范围返回key集合中元素(正序)</td> <td>zrangebyscore players  200 300  withscores</td></tr> <tr><td>zrevrangebyscore key  min  max  [withscores]</td> <td>按[min, max) 分数范围返回key集合中元素(倒序)</td> <td>zrevrangebyscore players  200 300  withscores</td></tr> <tr><td>zrem key member</td> <td>删除key集合中member元素与分数</td> <td>zrem players  a</td></tr> <tr><td>zremrangebyscore  key min max  withscores</td> <td>按[min, max) 分数范围删除key集合中元素</td> <td>zremrangebyscore  players  200 300  withscores</td></tr> <tr><td>zremrangebyrank  key start  stop</td> <td>删除key集合正序排名落在[start, stop) 范围元素</td> <td>zremrangebyrank  players  10  20</td></tr> <tr><td>zcount key min max</td> <td>按照分数范围[min, max]统计key集合中元素个数</td> <td>zcount  players  100 300</td></tr> <tr><td>ZUNIONSTORE</td> <td>计算给定的一个或多个有序集的并集</td> <td></td></tr> <tr><td>ZINTERSTORE</td> <td>计算给定的一个或多个有序集的交集</td> <td></td></tr></tbody></table> <h4 id="_4-5-3-应用场景"><a href="#_4-5-3-应用场景" class="header-anchor">#</a> 4.5.3 应用场景</h4> <ul><li><p><strong>排行榜系统</strong></p> <p>有序集合比较典型的使用场景就是排行榜系统。例如学生成绩的排名。某视频(博客等)网站的用户点赞、播放排名、电商系统中商品的销量排名等。我们以博客点赞为例。</p> <ul><li><p>添加用户赞数</p> <p>例如小编Tom发表了一篇博文，并且获得了10个赞。</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>zadd user:ranking article1 <span class="token number">10</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>取消用户赞数</p> <p>这个时候有一个读者又觉得Tom写的不好，又取消了赞，此时需要将文章的赞数从榜单中减去1，可以使用zincrby。</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>zincrby user:ranking <span class="token parameter variable">-1</span> article1 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>查看某篇文章的赞数</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>ZSCORE user:ranking arcticle1 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>展示获取赞数最多的十篇文章</p> <p>此功能使用zrevrange命令实现：</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>zrevrange user:ranking <span class="token number">0</span> <span class="token number">10</span>  <span class="token comment">#0 到 10表示元素个数索引</span>
zrevrangebyscore user:ranking <span class="token number">99</span> <span class="token number">0</span> <span class="token comment">##  按照分数从高到低排名，99，0表示score</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li></ul></li></ul> <h2 id="五、全局命令"><a href="#五、全局命令" class="header-anchor">#</a> 五、全局命令</h2> <p>全局命令针对的是所有的key.</p> <table><thead><tr><th>命令格式</th> <th>功能</th> <th>案例</th></tr></thead> <tbody><tr><td>keys  pattern</td> <td>按照pattern 匹配规则，列表redis中所有的key</td> <td>keys xxx:*</td></tr> <tr><td>exists  key</td> <td>判断key是否存在</td> <td>exists name</td></tr> <tr><td>expire key  seconds</td> <td>给key设置过期时间，超时：seconds</td> <td>expire name 10</td></tr> <tr><td>persist key</td> <td>取消key过期时间</td> <td>persist  name</td></tr> <tr><td>select  index</td> <td>切换数据库，默认是第0个，共有【0,15】个</td> <td>select 0</td></tr> <tr><td>move key   db</td> <td>从当前数据库将key移动到指定db库</td> <td>move name 1</td></tr> <tr><td>randomkey</td> <td>随机返回一个key</td> <td>randomkey</td></tr> <tr><td>rename key newkey</td> <td>将key改名为newkey</td> <td>rename name  newname</td></tr> <tr><td>echo message</td> <td>打印message信息</td> <td>echo  message</td></tr> <tr><td>dbsize</td> <td>查看key个数</td> <td>dbsize</td></tr> <tr><td>info</td> <td>查看redis数据库信息</td> <td>info</td></tr> <tr><td>config get  *</td> <td>查看所有redis配置信息</td> <td>config get *</td></tr> <tr><td>flushdb</td> <td>清空当前数据库</td> <td>flushdb</td></tr> <tr><td>flushall</td> <td>清空所有数据库</td> <td>flushall</td></tr></tbody></table> <h2 id="六、redis的安全性"><a href="#六、redis的安全性" class="header-anchor">#</a> 六、Redis的安全性</h2> <p>我们可以通过设置密码以及绑定IP的方式来增加Redis的安全性，这样可以限制未授权的访问和潜在的攻击源</p> <p>我们可以在window中找到<code>redis.windows-service.conf</code>文件,修改配置信息</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>requirepass wolfcode
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>在服务中重启Redis</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240111155707967.png" alt=""></strong></p> <p>如果我们访问的时候，没有携带密码登录，会提示如下信息</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240111160019740.png" alt=""></strong></p> <p>我们在连接到Redis的时候给他设置密码</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240111160106964.png" alt=""></strong></p> <h2 id="七、java客户端操作redis"><a href="#七、java客户端操作redis" class="header-anchor">#</a> 七、Java客户端操作Redis</h2> <p>当前Redis支持的编程语言有：https://Redis.io/docs/clients/</p> <p>目前市面上有很多的Redis的Java客户端框架，我们选取两款比较常用的给大家演示如何使用Java客户端操作Redis</p> <h3 id="_7-1-jedis客户端"><a href="#_7-1-jedis客户端" class="header-anchor">#</a> 7.1 Jedis客户端</h3> <ol><li><p>创建项目，添加依赖</p> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>parent</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-parent<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>2.3.3.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>relativePath</span><span class="token punctuation">/&gt;</span></span> <span class="token comment">&lt;!-- lookup parent from repository --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>parent</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">&gt;</span></span>test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>redis.clients<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>jedis<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>3.3.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.projectlombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>lombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br></div></div></li> <li><p>添加配置信息</p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code>
<span class="token comment">#Redis配置--jedis版</span>
<span class="token key atrule">jedis</span><span class="token punctuation">:</span>
  <span class="token key atrule">pool</span><span class="token punctuation">:</span>
    <span class="token comment">#Redis服务器的IP</span>
    <span class="token key atrule">host</span><span class="token punctuation">:</span> localhost
    <span class="token comment">#Redis服务器的Port</span>
    <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">6379</span>
    <span class="token comment">#数据库密码</span>
    <span class="token key atrule">password</span><span class="token punctuation">:</span>
    <span class="token comment">#连接超时时间</span>
    <span class="token key atrule">timeout</span><span class="token punctuation">:</span> <span class="token number">7200</span>
    <span class="token comment">#最大活动对象数</span>
    <span class="token key atrule">maxTotall</span><span class="token punctuation">:</span> <span class="token number">100</span>
    <span class="token comment">#最大能够保持idel状态的对象数</span>
    <span class="token key atrule">maxIdle</span><span class="token punctuation">:</span> <span class="token number">100</span>
    <span class="token comment">#最小能够保持idel状态的对象数</span>
    <span class="token key atrule">minIdle</span><span class="token punctuation">:</span> <span class="token number">50</span>
    <span class="token comment">#当池内没有返回对象时，最大等待时间</span>
    <span class="token key atrule">maxWaitMillis</span><span class="token punctuation">:</span> <span class="token number">10000</span>
    <span class="token comment">#当调用borrow Object方法时，是否进行有效性检查</span>
    <span class="token key atrule">testOnBorrow</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
    <span class="token comment">#当调用return Object方法时，是否进行有效性检查</span>
    <span class="token key atrule">testOnReturn</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
    <span class="token comment">#“空闲链接”检测线程，检测的周期，毫秒数。如果为负值，表示不运行“检测线程”。默认为-1.</span>
    <span class="token key atrule">timeBetweenEvictionRunsMillis</span><span class="token punctuation">:</span> <span class="token number">30000</span>
    <span class="token comment">#向调用者输出“链接”对象时，是否检测它的空闲超时；</span>
    <span class="token key atrule">testWhileIdle</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
    <span class="token comment">## 对于“空闲链接”检测线程而言，每次检测的链接资源的个数。默认为3.</span>
    <span class="token key atrule">numTestsPerEvictionRun</span><span class="token punctuation">:</span> <span class="token number">50</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div></li> <li><p>编写Jedis配置类</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Configuration</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">JedisConfig</span> <span class="token punctuation">{</span>
    <span class="token comment">/**
     * jedis连接池
     * @param jedisProperties
     * @return
     */</span>
    <span class="token annotation punctuation">@Bean</span>
    <span class="token keyword">public</span> <span class="token class-name">JedisPool</span> <span class="token function">jedisPool</span><span class="token punctuation">(</span><span class="token class-name">JedisProperties</span> jedisProperties<span class="token punctuation">)</span> <span class="token punctuation">{</span>

        <span class="token class-name">JedisPoolConfig</span> config <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">JedisPoolConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        config<span class="token punctuation">.</span><span class="token function">setMaxTotal</span><span class="token punctuation">(</span>jedisProperties<span class="token punctuation">.</span><span class="token function">getMaxTotall</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        config<span class="token punctuation">.</span><span class="token function">setMaxIdle</span><span class="token punctuation">(</span>jedisProperties<span class="token punctuation">.</span><span class="token function">getMaxIdle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        config<span class="token punctuation">.</span><span class="token function">setMinIdle</span><span class="token punctuation">(</span>jedisProperties<span class="token punctuation">.</span><span class="token function">getMinIdle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        config<span class="token punctuation">.</span><span class="token function">setMaxWaitMillis</span><span class="token punctuation">(</span>jedisProperties<span class="token punctuation">.</span><span class="token function">getMaxWaitMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        config<span class="token punctuation">.</span><span class="token function">setTestOnBorrow</span><span class="token punctuation">(</span>jedisProperties<span class="token punctuation">.</span><span class="token function">isTestOnBorrow</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        config<span class="token punctuation">.</span><span class="token function">setTestOnReturn</span><span class="token punctuation">(</span>jedisProperties<span class="token punctuation">.</span><span class="token function">isTestOnReturn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        config<span class="token punctuation">.</span><span class="token function">setTimeBetweenEvictionRunsMillis</span><span class="token punctuation">(</span>jedisProperties<span class="token punctuation">.</span><span class="token function">getTimeBetweenEvictionRunsMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        config<span class="token punctuation">.</span><span class="token function">setTestWhileIdle</span><span class="token punctuation">(</span>jedisProperties<span class="token punctuation">.</span><span class="token function">isTestWhileIdle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        config<span class="token punctuation">.</span><span class="token function">setNumTestsPerEvictionRun</span><span class="token punctuation">(</span>jedisProperties<span class="token punctuation">.</span><span class="token function">getNumTestsPerEvictionRun</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">StringUtils</span><span class="token punctuation">.</span><span class="token function">hasText</span><span class="token punctuation">(</span>jedisProperties<span class="token punctuation">.</span><span class="token function">getPassword</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">JedisPool</span><span class="token punctuation">(</span>config<span class="token punctuation">,</span> jedisProperties<span class="token punctuation">.</span><span class="token function">getHost</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> jedisProperties<span class="token punctuation">.</span><span class="token function">getPort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                    jedisProperties<span class="token punctuation">.</span><span class="token function">getTimeout</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> jedisProperties<span class="token punctuation">.</span><span class="token function">getPassword</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">JedisPool</span><span class="token punctuation">(</span>config<span class="token punctuation">,</span> jedisProperties<span class="token punctuation">.</span><span class="token function">getHost</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> jedisProperties<span class="token punctuation">.</span><span class="token function">getPort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                jedisProperties<span class="token punctuation">.</span><span class="token function">getTimeout</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br></div></div></li> <li><p>编写测试类，实现测试</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>jedisPool<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Jedis</span> jedis <span class="token operator">=</span> jedisPool<span class="token punctuation">.</span><span class="token function">getResource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//需求：往Redis中添加kv对： name：dafei</span>
    jedis<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;wolfcode&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>jedis<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    jedis<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div></li></ol> <h3 id="_7-2-lettuce"><a href="#_7-2-lettuce" class="header-anchor">#</a> 7.2 Lettuce</h3> <p>Lettuce 之所以能流行，因为它抱了根好大腿spring-data。spring-data-redis底层就封装了Lettuce，接下来看下SpringBoot版的lettuce实现。</p> <ol><li>添加依赖</li></ol> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-data-redis<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><ol start="2"><li>添加配置</li></ol> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">redis</span><span class="token punctuation">:</span>
    <span class="token key atrule">host</span><span class="token punctuation">:</span> 127.0.0.1
    <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">6379</span>
    <span class="token key atrule">password</span><span class="token punctuation">:</span> wolfcode
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><ol start="3"><li>编写测试类</li></ol> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@SpringBootTest</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">LettureTest</span> <span class="token punctuation">{</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">StringRedisTemplate</span> template<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testRedis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        template<span class="token punctuation">.</span><span class="token function">opsForValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">&quot;nickname&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;java&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>template<span class="token punctuation">.</span><span class="token function">opsForValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">&quot;nickname&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 操作string</span>
        <span class="token comment">//template.opsForValue().xx();</span>
        <span class="token comment">// 操作hash</span>
        <span class="token comment">//template.opsForHash().xx();</span>
        <span class="token comment">// 操作list</span>
        <span class="token comment">//template.opsForList().xx();</span>
        <span class="token comment">// 操作set</span>
        <span class="token comment">//template.opsForSet().xx();</span>
        <span class="token comment">// 操作zset</span>
        <span class="token comment">//template.opsForZSet().xx();</span>
        <span class="token comment">//全局命令在template类上</span>
        <span class="token comment">//template.keys(&quot;*&quot;);</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><h2 id="八、redis事务-拓展"><a href="#八、redis事务-拓展" class="header-anchor">#</a> 八、Redis事务[拓展]</h2> <p>一个事务从开始到执行会经历以下三个阶段：</p> <ul><li>开始事务。</li> <li>命令入队。</li> <li>执行事务。</li></ul> <p>它先以 <strong>MULTI</strong> 开始一个事务， 然后将多个命令入队到事务中， 最后由 <strong>EXEC</strong> 命令触发事务， 一并执行事务中的所有命令：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>Redis <span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> MULTI
OK

Redis <span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> SET book-name <span class="token string">&quot;Mastering C++ in 21 days&quot;</span>
QUEUED

Redis <span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> GET book-name
QUEUED

Redis <span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> SADD tag <span class="token string">&quot;C++&quot;</span> <span class="token string">&quot;Programming&quot;</span> <span class="token string">&quot;Mastering Series&quot;</span>
QUEUED

Redis <span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> SMEMBERS tag
QUEUED

Redis <span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> EXEC
<span class="token number">1</span><span class="token punctuation">)</span> OK
<span class="token number">2</span><span class="token punctuation">)</span> <span class="token string">&quot;Mastering C++ in 21 days&quot;</span>
<span class="token number">3</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>integer<span class="token punctuation">)</span> <span class="token number">3</span>
<span class="token number">4</span><span class="token punctuation">)</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token string">&quot;Mastering Series&quot;</span>
   <span class="token number">2</span><span class="token punctuation">)</span> <span class="token string">&quot;C++&quot;</span>
   <span class="token number">3</span><span class="token punctuation">)</span> <span class="token string">&quot;Programming&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><p>单个 Redis 命令的执行是原子性的，但 Redis 没有在事务上增加任何维持原子性的机制，所以 Redis 事务的执行并不是原子性的。</p> <p>Redis事务可以理解为一个<strong>打包的批量执行脚本</strong>，但批量指令并非原子化的操作，中间某条指令的失败不会导致前面已做指令的回滚，也不会造成后续的指令不做。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> multi
OK
<span class="token number">127.0</span>.0.1:6379<span class="token punctuation">(</span>TX<span class="token punctuation">)</span><span class="token operator">&gt;</span> <span class="token builtin class-name">set</span> name dafei
QUEUED
<span class="token number">127.0</span>.0.1:6379<span class="token punctuation">(</span>TX<span class="token punctuation">)</span><span class="token operator">&gt;</span> <span class="token builtin class-name">set</span> age <span class="token number">18</span>
QUEUED
<span class="token number">127.0</span>.0.1:6379<span class="token punctuation">(</span>TX<span class="token punctuation">)</span><span class="token operator">&gt;</span> incr age 
QUEUED
<span class="token number">127.0</span>.0.1:6379<span class="token punctuation">(</span>TX<span class="token punctuation">)</span><span class="token operator">&gt;</span> incr name
QUEUED
<span class="token number">127.0</span>.0.1:6379<span class="token punctuation">(</span>TX<span class="token punctuation">)</span><span class="token operator">&gt;</span> get age
QUEUED
<span class="token number">127.0</span>.0.1:6379<span class="token punctuation">(</span>TX<span class="token punctuation">)</span><span class="token operator">&gt;</span> get name
QUEUED
<span class="token number">127.0</span>.0.1:6379<span class="token punctuation">(</span>TX<span class="token punctuation">)</span><span class="token operator">&gt;</span> <span class="token builtin class-name">exec</span>
<span class="token number">1</span><span class="token punctuation">)</span> OK
<span class="token number">2</span><span class="token punctuation">)</span> OK
<span class="token number">3</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>integer<span class="token punctuation">)</span> <span class="token number">19</span>
<span class="token number">4</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> ERR value is not an integer or out of range
<span class="token number">5</span><span class="token punctuation">)</span> <span class="token string">&quot;19&quot;</span>
<span class="token number">6</span><span class="token punctuation">)</span> <span class="token string">&quot;dafei&quot;</span>
<span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><p>Redis 事务可以一次执行多个命令， 并且带有以下三个重要的保证：</p> <ul><li>批量操作在发送 EXEC 命令前被放入队列缓存。</li> <li>收到 EXEC 命令后进入事务执行，事务中任意命令执行失败，其余的命令依然被执行。</li> <li>在事务执行过程，其他客户端提交的命令请求不会插入到事务执行命令序列中。</li></ul> <h2 id="九、redis持久化机制-拓展"><a href="#九、redis持久化机制-拓展" class="header-anchor">#</a> 九、Redis持久化机制[拓展]</h2> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240111164446323.png" alt=""></strong></p> <p>虽然Redis的数据存储到内存中，但是也会不定期的往磁盘中进行同步，保证了Redis在重启之后，不至于所有数据都丢失.</p> <p>Redis持久化机制目前以后3种，分别为：</p> <ul><li><p><strong>快照方式</strong>（RDB, Redis DataBase）</p></li> <li><p><strong>文件追加方式</strong>（AOF, Append Only File）</p></li> <li><p><strong>混合持久化方式</strong>（Redis4版本之后）</p></li></ul> <h3 id="_9-1-rdb方式"><a href="#_9-1-rdb方式" class="header-anchor">#</a> 9.1 RDB方式</h3> <p>Snapshotting(快照)默认方式，将内存数据中以快照的方式写入到二进制文件中，默认为dump.rdb。触发RDB持久化过程分手动触发与自动触发。</p> <p><strong>触发机制</strong></p> <ul><li><p><strong>手动触发</strong></p> <p>使用save命令：会阻塞当前Redis服务器，知道RDB过程完成为主，如果内存数据较多，会造成长时间阻塞，影响其他命令的使用，不建议轻易使用</p> <p>使用bgsave命令：Redis进程执行fork指令创建子进程，由子进程实现RDB持久化，有需要时建议使用bgsave命令。</p></li> <li><p><strong>自动触发</strong></p> <p>使用save相关配置，格式： save m n      表示m秒内数据集存在n次修改时会自动触发bgsave命令。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>save 900 1  #900秒内如果超过1个Key被修改则发起快照保存
save 300 10 #300秒内如果超过10个key被修改,则发起快照保存
save 60 10000
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240111165401272.png" alt=""></strong></p></li></ul> <p><strong>优点:</strong></p> <ul><li><p>RDB快照文件是一个紧凑压缩的二进制文件，非常使用用于备份，全量复制等场景。开发中可以按照每6小时执行一次bgsave备份，用于容灾备份。</p></li> <li><p>Redis加载RDB恢复数据远远快于AOF方式</p></li></ul> <p><strong>缺点:</strong></p> <ul><li>RDB无法做到实时持久化/秒级持久化，每次bgsave时都需要fork子进程，频繁执行有时间成本。</li> <li>RDB快照文件不同版本格式不一样，容易引起兼容问题。</li></ul> <h3 id="_9-2-aof方式"><a href="#_9-2-aof方式" class="header-anchor">#</a> 9.2 AOF方式</h3> <p>AOF与RDB不一样，它是一独立日志的方式记录每次写命令，重启时再重新执行AOF文件中命令达到恢复数据的目的。解决了数据持久化的实时性的问题。</p> <p>Redis默认是不开启的，需要使用时，需要配置： <strong>appendonly yes</strong></p> <table><thead><tr><th>策略</th> <th>解释</th></tr></thead> <tbody><tr><td>appendfsync always</td> <td>收到命令就立即写到磁盘,效率最慢.但是能保证完全的持久化</td></tr> <tr><td>appendfsync everysec</td> <td>每秒写入磁盘一次,在性能和持久化方面做了很好的折中</td></tr> <tr><td>appendfsync no</td> <td>完全以依赖os，一般同步周期是30秒</td></tr></tbody></table> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240111165540632.png" alt=""></strong></p> <p><strong>优点:</strong></p> <ul><li><p>AOF方式数据安全性更高，配置得当，最多损失1秒的数据量</p></li> <li><p>在不小心执行flushall命令，也可以通过AOF方式恢复(删除最后一个命令即可)</p></li> <li><p>AOF 日志是一个增量日志文件，不会存在断电时出现损坏问题。即使出现问题，Redis-check-aof 工具也能够轻松修复它。</p></li> <li><p>当 AOF 变得太大时，Redis 能够在后台自动重写 AOF</p></li></ul> <p><strong>缺点:</strong></p> <ul><li>相同数据量来说，AOF文件体积通常大于RDB文件</li> <li>数据持久化性能上来说，AOF 比 RDB 慢</li></ul> <h3 id="_9-3-rdb-aof混合方式"><a href="#_9-3-rdb-aof混合方式" class="header-anchor">#</a> 9.3 RDB-AOF混合方式</h3> <p>Redis 4.0之后，推出了混合持久化方式，他把RDB,AOF的优点集中起来，简单的说就是在你执行bgrewriteaof或触发自动AOF重写的时候，将AOF文件以RDB文件格式（二进制压缩）写到AOF文件中。而在下次重写之前，依然用aof文件格式追加到上面二进制数据的后面。注意： 开启它之前必须现开启AOF</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>aof-use-rdb-preamble yes
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>这样既可以让数据恢复时和RDB一样快速，又可以和AOF文件一样准确。兼顾了性能和安全性。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240111165901039.png" alt=""></strong></p> <h2 id="十、redis内存淘汰机制-拓展"><a href="#十、redis内存淘汰机制-拓展" class="header-anchor">#</a> 十、Redis内存淘汰机制[拓展]</h2> <h3 id="_10-1-引言"><a href="#_10-1-引言" class="header-anchor">#</a> 10.1 引言</h3> <p>Redis 启动会加载一个配置：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>maxmemory <span class="token operator">&lt;</span>byte<span class="token operator">&gt;</span>   //内存上限
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>默认值为 0 (window版的限制为100M)，表示默认设置Redis内存上限。但是真实开发还是需要提前评估key的体量，提前设置好内容上限。</p> <p>此时思考一个问题，开发中，在设置完内存上限之后，如果Redis key达到上限了，该怎么办？这就设置到Redis的内存淘汰机制了。</p> <h3 id="_10-2-内存淘汰算法"><a href="#_10-2-内存淘汰算法" class="header-anchor">#</a> 10.2 内存淘汰算法</h3> <p>Redis内存淘汰机制也可以称之为key内卷机制，当资源不足时，该如何选择？</p> <p>常见的内存淘汰机制分为四大类：</p> <ul><li>**LRU：**LRU是Least recently used，最近最少使用的意思，简单的理解就是从数据库中删除最近最少访问的数据，该算法认为，你长期不用的数据，那么被再次访问的概率也就很小了，淘汰的数据为最长时间没有被使用，仅与时间相关。</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220902161050652.png" alt=""></p> <ul><li><p>**LFU：**LFU是Least Frequently Used，最不经常使用的意思，简单的理解就是淘汰一段时间内，使用次数最少的数据，这个与频次和时间相关。</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220902161057116.png" alt=""></p></li> <li><p>**TTL：**Redis中，有的数据是设置了过期时间的，而设置了过期时间的这部分数据，就是该算法要解决的对象。如果你快过期了，不好意思，我内存现在不够了，反正你也要退休了，提前送你一程，把你干掉吧。</p></li> <li><p>随机淘汰：生死有命，富贵在天，是否被干掉，全凭天意了。</p></li></ul> <h3 id="_10-3-redis淘汰策略"><a href="#_10-3-redis淘汰策略" class="header-anchor">#</a> 10.3 Redis淘汰策略</h3> <p>Redis 通过配置</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>maxmemroy-policy  
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>来配置指定具体的淘汰机制，可供选择的值有：</p> <p>通过maxmemroy-policy可以配置具体的淘汰机制，看了网上很多文章说只有6种，其实有8种，可以看Redis5.0的配置文件，上面有说明：</p> <ul><li><p>volatile-lru -&gt; 找出已经设置过期时间的数据集，将最近最少使用（被访问到）的数据干掉。</p></li> <li><p>volatile-ttl -&gt; 找出已经设置过期时间的数据集，将即将过期的数据干掉。</p></li> <li><p>volatile-random -&gt; 找出已经设置过期时间的数据集，进行无差别攻击，随机干掉数据。</p></li> <li><p>volatile-lfu -&gt; 找出已经设置过期时间的数据集，将一段时间内，使用次数最少的数据干掉。</p></li> <li><p>allkeys-lru -&gt;与第1个差不多，数据集从设置过期时间数据变为全体数据。</p></li> <li><p>allkeys-lfu -&gt; 与第4个差不多，数据集从设置过期时间数据变为全体数据。</p></li> <li><p>allkeys-random -&gt; 与第3个差不多，数据集从设置过期时间数据变为全体数据。</p></li> <li><p>no-enviction -&gt; 什么都不干，报错，告诉你内存不足，这样的好处是可以保证数据不丢失</p></li></ul> <p><strong>系统默认选择： noenviction</strong></p> <h2 id="十一、过期key处理-拓展"><a href="#十一、过期key处理-拓展" class="header-anchor">#</a> 十一、过期Key处理[拓展]</h2> <p>接下讨论一个问题：Redis的key过期了，该如何清理问题。</p> <p>Redis给出3种实现方案：</p> <p><strong>惰性删除</strong>：当访问Key时，才去判断它是否过期，如果过期，直接干掉。这种方式对CPU很友好，但是一个key如果长期不用，一直存在内存里，会造成内存浪费。</p> <p><strong>定时删除</strong>：设置键的过期时间的同时，创建一个定时器，当到达过期时间点，立即执行对Key的删除操作，这种方式对CPU不友好，得额外让出CPU维护定时器。</p> <p><strong>定期删除</strong>：隔一段时间，对数据进行一次检查，删除里面的过期Key，至于要删除多少过期Key，检查多少数据，则由算法决定。</p> <p><strong>Redis服务器实际使用的是惰性删除和定期删除两种策略：通过配合使用这两种删除策略，可以很好地在合理使用CPU和避免浪费内存之间取得平衡。</strong></p> <h2 id="十二、小结与作业"><a href="#十二、小结与作业" class="header-anchor">#</a> 十二、小结与作业</h2> <h3 id="_12-1-小结"><a href="#_12-1-小结" class="header-anchor">#</a> 12.1 小结</h3> <ul><li>掌握Redis基本数据结构</li> <li>掌握Redis常用命令</li> <li>了解Redis事务</li> <li>了解Redis持久化机制</li> <li>了解Redis内存淘汰机制</li> <li>了解Redis key过期处理机制</li></ul> <h3 id="_12-2-作业"><a href="#_12-2-作业" class="header-anchor">#</a> 12.2 作业</h3> <ul><li>命令5遍</li> <li>小案例</li></ul> <p>类似效果：https://www.mafengwo.cn/gonglve/ziyouxing/2339.html</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220902213752150.png" alt=""></p></div></div>  <div class="page-edit"><!----> <div class="tags"><a href="/tags/?tag=Redis" title="标签">#Redis</a></div> <!----></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/9927d2/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">SpringSecurity</div></a> <a href="/pages/4c087b/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">MongoDB</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/9927d2/" class="prev">SpringSecurity</a></span> <span class="next"><a href="/pages/4c087b/">MongoDB</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/0c07b2/"><div>
            IT杂货铺
            <!----></div></a> <span class="date">03-23</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/705b35/"><div>
            人事百问
            <!----></div></a> <span class="date">03-22</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/78eb56/"><div>
            MyBatis-Plus
            <!----></div></a> <span class="date">03-12</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:langfeiyes@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/langfeiyes" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2024-2024
    <span><a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备18007927号-1</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.a2a00aa5.js" defer></script><script src="/assets/js/2.256f807a.js" defer></script><script src="/assets/js/60.88d94274.js" defer></script>
  </body>
</html>
